package com.gopay.bis.manager.bank.ebank;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.abc.ebank.AbcB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 农行网关B2B对象封装
 * 
 * @author AbcB2bManager.java
 * @author LiLiLi
 */
public class AbcB2bManager implements BankManagerRemoteService {

	@Resource(name = "abcConfig")
	private Map<String, String> abcConfig;
	
	@Resource(name = "abcB2bBankServiceClient")
	private BankService abcB2bBankServiceClient;
	
	@Resource(name = "abcB2bBankServiceClientHna")
	private BankService abcB2bBankServiceClientHna;

	/**
	 * B2B签名
	 * 
	 * @param BaseMessage
	 * @return Object
	 * @throws GopayException
	 */
	public Object sign(BaseMessage message) throws GopayException {
		AbcB2bMessage messageB2B = (AbcB2bMessage) message;
		messageB2B.setPayTypeID((String) abcConfig.get("ABC.B2B.PAYTYPEID"));
		Date orderDate = DateUtils.strToDate(messageB2B.getOrderDate());
		messageB2B.setTrnxDate(DateUtils.format(orderDate, "yyyy/MM/dd"));
		messageB2B.setTrnxTime(DateUtils.format(orderDate, "HH:mm:ss"));
		messageB2B.setCurrencyCode((String)abcConfig.get("ABC.B2B.CURRENCYCODE"));
		messageB2B.setInstallmentMark((String)abcConfig.get("ABC.B2B.INSTALLMENTMARK"));
		messageB2B.setCommodityType((String)abcConfig.get("ABC.B2B.COMMODITYTYPE"));
		messageB2B.setMerchantRemarks((String)abcConfig.get("ABC.B2B.MERCHANTREMARKS"));
        if (StringUtils.isNotBlank(messageB2B.getPaymentAccInfo())) {
            messageB2B.setMerchantRemarks(message.getPaymentAccInfo());
        }
		messageB2B.setResultNotifyURL(messageB2B.getPayRuturnUrl());
		messageB2B.setPaymentType((String) abcConfig.get("ABC.B2B.PAYMENTTYPE"));
		messageB2B.setPaymentLinkType((String) abcConfig.get("ABC.B2B.PAYMENTLINKTYPE"));
		messageB2B.setNotifyType((String) abcConfig.get("ABC.B2B.NOTIFYTYPE"));;
		messageB2B.setIsBreakAccount((String) abcConfig.get("ABC.B2B.ISBREAKACCOUNT"));		
		// 调用远程接口请求签名数据
		BankLogUtils.printB2BManagerSignSendLog(messageB2B);
		messageB2B = (AbcB2bMessage) abcB2bBankServiceClient.sign(messageB2B);			
		BankLogUtils.printB2BManagerSignReturnLog(messageB2B);
		return messageB2B;
	}

	/**
	 * B2B验签
	 * 
	 * @param BaseMessage
	 * @return Object
	 * @throws GopayException
	 */
	public Object verify(BaseMessage message) throws GopayException {
		AbcB2bMessage messageB2B = (AbcB2bMessage) message;
		messageB2B.setOutStlmCd(BankConstants.BANK_LOGO_ABC);
		BankLogUtils.printB2BManagerVerifySendLog(messageB2B);
        messageB2B = (AbcB2bMessage) abcB2bBankServiceClient.verify(messageB2B);	
		BankLogUtils.printB2BManagerVerifyReturnLog(messageB2B, messageB2B);
		return messageB2B;
	}

	/**
	 * B2B订单查询
	 * 
     * @param Object
     * @return Object
     * @throws GopayException
	 */
	public Object query(Object message) throws GopayException {
		AbcB2bMessage messageB2B = (AbcB2bMessage) message;
		messageB2B.setOutStlmCd(BankConstants.BANK_LOGO_ABC);
		messageB2B.setPayTypeID((String) abcConfig.get("ABC.B2B.PAYTYPEID"));
		messageB2B.setQueryDetail((String) abcConfig.get("ABC.B2B.QUERYDETAIL")); 
		BankLogUtils.printB2BManagerQuerySendLog(messageB2B, messageB2B);		
	    messageB2B = (AbcB2bMessage) abcB2bBankServiceClient.query(messageB2B);			
		BankLogUtils.printB2BManagerQueryReturnLog(messageB2B);
		return messageB2B;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
